
package com.code.aspectj.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.CodeSignature;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.code.aspectj.AppInterceptor.TAG;


/**
 * Created by RLZ
 * on 2021/12/6
 */
public final class ReflectUtils {

    private ReflectUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 根据分隔符将List转换为String
     */
    public static String listToString(final List<String> list, final String separator) {
        if (list == null || list.size() == 0) return "";

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

    /**
     * 根据分隔符将List转换为String
     */
    public static String listToString(final List<String> list) {
        return listToString(list, ",");
    }


    public static String getClassName(Class<?> cls) {
        if (cls.isAnonymousClass()) {
            return getClassName(Objects.requireNonNull(cls.getEnclosingClass()));
        }
        return cls.getSimpleName();
    }

    /**
     * 获取方法名
     */
    public static String getMethodName(final ProceedingJoinPoint joinPoint) {
        CodeSignature c = (CodeSignature) joinPoint.getSignature();
        Class<?> cls = c.getDeclaringType(); //方法所在类
        String m = c.getName();    //方法名
        return ReflectUtils.getClassName(cls) + "." + m;
    }

    /**
     * 获取方法的描述信息
     */
    public static String getMethodDescribeInfo(final ProceedingJoinPoint joinPoint) {
        CodeSignature c = (CodeSignature) joinPoint.getSignature();
        Class<?> cls = c.getDeclaringType();    // 方法所在类
        String m = c.getName();        // 方法名
        return ReflectUtils.getClassName(cls) + "->" + m;
    }


    /**
     * 请求数据 不需要返回信息
     */
    public static void invoke(Map<String, String> map, String url) {
        try {
            //获取网络请求工具
            Class<?> httpClass = Class.forName("com.seca.live.okhttp.HttpUtils");

            Method method = httpClass.getMethod("post",
                    java.lang.String.class,
                    java.lang.Object.class,
                    java.util.Map.class,
                    java.lang.Object.class);

            Method commonParams = httpClass.getMethod("getCommonParams");

            Object commonMap = commonParams.invoke(null);

            if (map.isEmpty()) {
                Map<String, String> stringMap = (Map<String, String>) commonMap;
                assert stringMap != null;
                map.putAll(stringMap);
            }

            //发送网络请求消息
            method.invoke(null, url, "", map, Class.forName("com.zhy.http.okhttp.callback.StringCallback"));
            P.d(TAG, "执行广告统计, 发送消息完成 = ");
        } catch (Exception e) {
            P.e(TAG, e.getMessage());
        }
    }
}
